﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FlashMessanger.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains FlashMessanger class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IBupro.Classes
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Flash messenger handles messages for user.
    /// </summary>
    public class FlashMessanger : IFlashMessenger
    {
        /// <summary>
        /// Key for session.
        /// </summary>
        private const string SessionKey = "FlashMessenger";

        /// <summary>
        /// Http session.
        /// </summary>
        private readonly Session session;

        public FlashMessanger()
        {
            this.session = new Session();
        }

        private class Message
        {
            public string Content { get; set; }
            public MessageType Type { get; set; }
        }

        private IList<Message> Messages
        {
            get
            {
                if (this.session[SessionKey] == null)
                {
                    var list = new List<Message>();
                    this.session[SessionKey] = list;
                    return list;
                }

                return (IList<Message>)this.session[SessionKey];
            }
        }

        /// <summary>
        /// Adds the message to flash messenger.
        /// </summary>
        /// <param name="content">
        /// The content.
        /// </param>
        /// <param name="messageType">
        /// The message Type.
        /// </param>
        public void AddMessage(string content, MessageType messageType)
        {
            this.Messages.Add(new Message()
                                  {
                                      Content = content,
                                      Type = messageType
                                  });
        }

        /// <summary>
        /// Gets the messages and cleans the queue.
        /// </summary>
        /// <param name="messageType">
        /// The message type.
        /// </param>
        /// <returns>
        /// The messages.
        /// </returns>
        public IEnumerable<string> GetMessages(MessageType messageType)
        {
            return this.Messages.Where(m => m.Type == messageType).Select(m => m.Content).ToArray();
        }

        /// <summary>
        /// Gets the messages and cleans the queue.
        /// </summary>
        /// <returns>
        /// The messages.
        /// </returns>
        public IEnumerable<string> GetMessages()
        {
            return this.Messages.Select(m => m.Content).ToArray();
        }

        /// <summary>
        /// Gets a value indicating whether this instance has messages.
        /// </summary>
        public bool HasMessages
        {
            get
            {
                return this.Messages.Any();
            }
        }

        public void RemoveAllMessages()
        {
            this.Messages.Clear();
        }
    }
}